package com.victor.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.victor.dao.BookcaseDao;
import com.victor.dao.PictureStaticDao;
import com.victor.entity.Bookcase;
import com.victor.service.BookcaseService;

@Service
@Transactional
public class BookcaseServiceImpl implements BookcaseService {

	@Autowired
	private BookcaseDao dao;

	@Autowired
	private PictureStaticDao staticDao;

	@Override
	public boolean batchDelete(int[] ids) {
		if (staticDao.updatebookcase(ids) < 0) {
			return false;
		}
		return dao.batchDelete(ids) > 0 ? true : false;
	}

	@Override
	public boolean insert(Bookcase record) {
		return dao.insert(record) > 0 ? true : false;
	}

	@Override
	public Bookcase findById(Integer bId) {
		return dao.findById(bId);
	}

	@Override
	public boolean update(Bookcase record) {
		return dao.update(record) > 0 ? true : false;
	}

	@Override
	public List<Map<String, String>> findByAttribute(Bookcase bookcase) {
		return dao.findByAttribute(bookcase);
	}

	@Override
	public List<Map<String, String>> findByUseridm(String userid, Integer limit, Integer btype, Integer pictype) {
		return dao.findByUseridm(userid, limit, btype,pictype);
	}

	@Override
	public List<Bookcase> findByParam(Bookcase bookcase) {
		return dao.findByParam(bookcase);
	}

	@Override
	public boolean batchInsert(List<Bookcase> list) {
		return dao.batchInsert(list) > 0 ? true : false;
	}

	@Override
	public void updateOverdue() {
		List<Bookcase> bookList = dao.findOverdue();
		List<Integer> bookids = bookList.stream().map(Bookcase::getBId).collect(Collectors.toList());
		int[] arr = new int[bookids.size()];
		for (int i = 0; i < bookids.size(); i++) {
			if (bookids.get(i) != null) {
				arr[i] = bookids.get(i);
			}
		}
		if (arr != null && arr.length > 0) {
			dao.batchDelete(arr);
		}
	}

	@Override
	public boolean contrastBySid(String sid, int userid) {
		List<String> sidList = dao.findSidByUserid(userid);
		if (sidList != null && !sidList.isEmpty()) {
			return sidList.stream().filter(c -> c.equals(sid)).count() > 0L ? false : true;
		}
		return true;
	}

}
